package edu.washington.apl.weather.util;

import java.io.FileInputStream;
import java.util.Properties;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;

import org.apache.log4j.Logger;

public class WeatherProperties extends Properties {

    private static final String DEVICE_DEFAULT = "COM3";
    private static final String WIND_RATE_DEFAULT = "5";
    private static final String INDOOR_RATE_DEFAULT = "1";
    private static final String OUTDOOR_RATE_DEFAULT = "1";
    private static final String RAIN_RATE_DEFAULT = "2";
    private static final String DB_ENABLE_DEFAULT = "false";
    private static final String DB_DRIVER_DEFAULT = "com.mysql.jdbc.Driver";
    private static final String DB_PROTOCOL_DEFAULT = "jdbc:mysql:";
    private static final String DB_SERVER_DEFAULT = "endif";
    private static final String DB_DATABASE_DEFAULT = "weather";
    private static final String DB_USERNAME_DEFAULT = "weather";
    private static final String DB_PASSWORD_DEFAULT = "wmr968";
    private static boolean rapidFireEnabled = false;
    private static final String RF_PASSWORD_DEFAULT = "";
    private static final String RF_STATIONID_DEFAULT = "";
    private static Logger logger = Logger.getLogger(WeatherProperties.class);
    private static String device = null;
    private static double windRate = 0;
    private static double indoorRate = 0;
    private static double outdoorRate = 0;
    private static double rainRate = 0;
    private static boolean enable = false;
    private static String driver = null;
    private static String protocol = null;
    private static String server = null;
    private static String database = null;
    private static String username = null;
    private static String password = null;
    private static String rfStationId = null;
    private static String rfStationPassword = null;
    private static WeatherProperties singleton = new WeatherProperties();

    private WeatherProperties() {
        try {
            load("weather.properties");
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(WeatherProperties.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(0);
        }
    }

    private void load(String pfile) throws FileNotFoundException, IOException {
        InputStream pstream = new FileInputStream(pfile);//WeatherProperties.class.getClassLoader().getResourceAsStream(pfile);
        if (pstream == null) {
            throw new FileNotFoundException("Could not find properties file: " + pfile);
        }

        load(pstream);

        device = getProperty("wmr.device", DEVICE_DEFAULT);

        windRate = Double.parseDouble(getProperty("wind.rate", WIND_RATE_DEFAULT));
        indoorRate = Double.parseDouble(getProperty("indoor.rate", INDOOR_RATE_DEFAULT));
        outdoorRate = Double.parseDouble(getProperty("outdoor.rate", OUTDOOR_RATE_DEFAULT));
        rainRate = Double.parseDouble(getProperty("rain.rate", RAIN_RATE_DEFAULT));

        enable = Boolean.parseBoolean(getProperty("db.enable", DB_ENABLE_DEFAULT));

        driver = getProperty("db.driver", DB_DRIVER_DEFAULT);
        protocol = getProperty("db.protocol", DB_SERVER_DEFAULT);
        server = getProperty("db.server", DB_SERVER_DEFAULT);
        database = getProperty("db.database", DB_DATABASE_DEFAULT);
        username = getProperty("db.username", DB_USERNAME_DEFAULT);
        password = getProperty("db.password", DB_PASSWORD_DEFAULT);
        rapidFireEnabled = Boolean.valueOf(getProperty("rf.enable", "false"));
        rfStationId = getProperty("rf.stationid", RF_STATIONID_DEFAULT);
        rfStationPassword = getProperty("rf.password", RF_PASSWORD_DEFAULT);
    }

    public static String getDevice() {
        return device;
    }

    public static double getWindRate() {
        return windRate;
    }

    public static double getIndoorRate() {
        return indoorRate;
    }

    public static double getOutdoorRate() {
        return outdoorRate;
    }

    public static double getRainRate() {
        return rainRate;
    }

    public static String getDriver() {
        return driver;
    }

    public static boolean isDatabaseEnabled() {
        return enable;
    }

    public static boolean isRapidFireEnabled() {
        return rapidFireEnabled;
    }

    public static String getRfStationId() {
        return rfStationId;
    }

    public static String getRfStationPassword() {
        return rfStationPassword;
    }

    public static String getConnectionString() {
        return protocol + "//" + server + "/" + database + "?user=" + username + "&password=" + password;
    }

    public static WeatherProperties getInstance() {
        return singleton;
    }
    
    public boolean getBooleanProperty(String name, boolean defaultValue) {   
        return Boolean.valueOf(getProperty(name, Boolean.toString(defaultValue)));
    }
}
